package gitobject;

import sha1.SHA1;
import zlib.ZLibUtils;
import java.io.*;

public class Blob extends GitObject{
    
	public String getFmt(){
        return fmt;
    }
    public String getMode(){
        return mode;
    }
    public static String getPath() {
        return path;
    }
    public String getValue(){
        return value;
    }
    public String getKey() {
        return key;
    }
    public Blob(){}
    /**
     * Constructing blob object from a file
     */
    public Blob(File file) throws Exception {
        fmt = "blob";
        mode = "100644";
        value = getValue(file);
        modified_time = file.lastModified();
        genKey(file);
        compressWrite();
    }

    /**
     * Deserialize a blob object from an existed hash file in .jit/objects.
     */
    public static Blob deserialize(String Id) {
        Blob result = new Blob();
        try{
        FileInputStream fis = new FileInputStream(path + File.separator +Id);
        byte[] dcp = ZLibUtils.decompress(fis);
        fis.close();
        ByteArrayInputStream bai = new ByteArrayInputStream(dcp);
        ObjectInputStream ois = new ObjectInputStream(bai);
        bai.close();
        result = (Blob)(ois.readObject());
        ois.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * Generate key from file.
     */
    public void genKey(File file) throws Exception {
        key = SHA1.getHash(file);
    }

    @Override
    public String toString(){
        return "100644 blob " + key;
    }
}
